package com.syp.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syp.lease.common.exception.LeaseException;
import com.syp.lease.common.result.ResultCodeEnum;
import com.syp.lease.model.entity.*;
import com.syp.lease.model.enums.ItemType;
import com.syp.lease.web.admin.mapper.*;
import com.syp.lease.web.admin.service.*;
import com.syp.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.syp.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.syp.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.syp.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.syp.lease.web.admin.vo.fee.FeeValueVo;
import com.syp.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

// 实现ApartmentInfoService接口的服务实现类
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    // 注入GraphInfoService用于处理图形信息
    @Autowired
    private GraphInfoService graphInfoService;

    // 注入ApartmentFacilityService用于处理公寓设施信息
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    // 注入ApartmentFeeValueService用于处理公寓费用信息
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    // 注入ApartmentLabelService用于处理公寓标签信息
    @Autowired
    private ApartmentLabelService apartmentLabelService;

    // 注入ApartmentInfoMapper用于公寓信息的数据库操作
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    // 注入GraphInfoMapper用于图形信息的数据库操作
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    // 注入FacilityInfoMapper用于设施信息的数据库操作
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    // 注入FeeValueMapper用于费用信息的数据库操作
    @Autowired
    private FeeValueMapper feeValueMapper;

    // 注入LabelInfoMapper用于标签信息的数据库操作
    @Autowired
    private LabelInfoMapper labelInfoMapper;

    // 注入RoomInfoMapper用于房间信息的数据库操作
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 保存或更新公寓信息
     *
     * @param apartmentSubmitVo 公寓提交信息对象，包含公寓的各种信息
     */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 判断是否为更新操作
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        if (isUpdate) {
            // 更新操作前，先删除原有的图片、设施、费用和标签信息
            // 删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);

            // 删除配套列表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            // 删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            // 删除标签列表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        }
        // 插入新的图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = GraphInfo.builder()
                        .itemId(apartmentSubmitVo.getId())
                        .itemType(ItemType.APARTMENT)
                        .name(graphVo.getName())
                        .url(graphVo.getUrl())
                        .build();
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        // 插入新的配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .facilityId(facilityInfoId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        // 插入新的杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
        // 插入新的标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .labelId(labelId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
    }

    /**
     * 根据查询条件分页获取公寓列表
     *
     * @param page    分页对象，包含页码和页大小信息
     * @param queryVo 查询条件对象，包含查询公寓的条件
     * @return 返回分页后的公寓列表
     */
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    /**
     * 根据ID获取公寓详情
     *
     * @param id 公寓ID
     * @return 返回公寓详情对象，包含公寓的详细信息
     */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        // 1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = getById(id);
        if (apartmentInfo == null) {
            return null;
        }
        // 2.查询GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        // 3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        // 4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        // 5.查询FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);
        // 将查询到的信息封装到ApartmentDetailVo对象中
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    /**
     * 根据ID删除公寓信息
     *
     * @param id 公寓ID
     */
    @Override
    public void removeApartmentById(Long id) {
        // 首先删除ApartmentInfo记录
        super.removeById(id);
        // 检查并删除关联的RoomInfo记录
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomInfo> wrapper = roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(wrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        // 删除关联的图片列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);
        // 删除关联的配套列表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
        // 删除关联的杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        // 删除关联的标签列表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
    }
}
